package com.zmn.oms.zmn.normal.business.impl.work;

import com.google.common.collect.Lists;
import com.zmn.common.constant.FicoConsts;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.model.dto.work.modify.amount.ZsChannelAmountDTO;
import com.zmn.oms.model.dto.work.modify.amount.ZsChannelPrepayDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.discount.OrderDiscount;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.discount.OrderDiscountService;
import com.zmn.oms.services.interfaces.work.OrderWorkAmountService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkAmountBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalOrderWorkAmountBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalWorkBService;
import com.zmn.pay.common.constant.PayConsts;
import com.zmn.pay.common.dto.PayResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：工单金额
 *
 * @author liuying
 * @date 2018/12/17 22:53
 */
@Service
public class ZsNormalOrderWorkAmountBServiceImpl implements ZsNormalOrderWorkAmountBService {

	@Autowired
	private OrderWorkAmountService orderWorkAmountService;
	@Autowired
	private ZsNormalWorkBService zsNormalWorkBService;
	@Autowired
	private ZsOrderWorkAmountBService zsOrderWorkAmountBService;
	@Autowired
	private OrderDetailService orderDetailService;
	@Autowired
	private OrderDiscountService orderDiscountService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	@ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_UPDATE_CHANNEL_AMOUNT)
	public void saveChannelAmount(ZsChannelAmountDTO zsChannelAmountDTO) {
		OrderWork orderWork = new OrderWork();
		orderWork.setOrderId(zsChannelAmountDTO.getOrderId());
		orderWork.setWorkId(zsChannelAmountDTO.getWorkId());
		if (zsChannelAmountDTO.getDeleteChannelAmount() == null || Objects.equals(zsChannelAmountDTO.getDeleteChannelAmount(), GlobalConsts.NO)) {
			orderWork.setChannelAmount(zsChannelAmountDTO.getChannelAmount());
		}
		orderWorkAmountService.updateChannelAmount(orderWork);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveChannelPrepay(ZsChannelPrepayDTO zsChannelPrepayDTO) {

		Long orderId = zsChannelPrepayDTO.getOrderId();
		Long workId = zsChannelPrepayDTO.getWorkId();

		OrderDetail orderDetail = orderDetailService .findOrderDetailByKey(orderId);
		Integer servCategId = orderDetail.getServCategId();

		// 2. 改支付状态
		OrderWork updateAmount = new OrderWork();
		updateAmount.setOrderId(orderId);
		updateAmount.setWorkId(workId);
		updateAmount.setPayStatus(PayConsts.PAY_STATUS_NONE);
		updateAmount.setChannelPrepayStatus(zsChannelPrepayDTO.getChannelPrepayStatus());
		updateAmount.setChannelPrepayAmount(zsChannelPrepayDTO.getChannelPrepayAmount());
		updateAmount.setOriginalAmount(zsChannelPrepayDTO.getOriginalAmount());
		// 优惠金额
		updateAmount.setDiscountAmount(Optional.ofNullable(zsChannelPrepayDTO.getDiscountAmount()).orElse(0));

		if (Objects.equals(GlobalConsts.SERV_CATEG_CLEAN, servCategId)) {

			if (updateAmount.getOriginalAmount() != null) {

				// 计算优惠
				updateAmount.setDiscountAmount(updateAmount.getOriginalAmount() - updateAmount.getChannelPrepayAmount());
			}else{

				// 计算原价
				if (updateAmount.getChannelPrepayAmount() != null) {
					updateAmount.setOriginalAmount(updateAmount.getChannelPrepayAmount() + updateAmount.getDiscountAmount());
				} else if (updateAmount.getTotalAmount() != null) {
					updateAmount.setOriginalAmount(updateAmount.getTotalAmount() + updateAmount.getDiscountAmount());
				}
			}

			// 清洗单设置金额
			if (Objects.equals(zsChannelPrepayDTO.getChannelPrepayStatus(), PayConsts.PAY_STATUS_DONE)) {

				// 已支付，设置金额，订单金额 = 预付 + 优惠；订单收入=预付
				updateAmount.setMasterAmount(0);
				updateAmount.setTotalAmount(updateAmount.getChannelPrepayAmount());
				updateAmount.setMasterPayStatus(PayConsts.PAY_STATUS_DONE);
				updateAmount.setPayStatus(PayConsts.PAY_STATUS_DONE);

			} else {

				// 设置应收、工程师收款金额
				if (zsChannelPrepayDTO.getTotalAmount() != null) {
					updateAmount.setTotalAmount(zsChannelPrepayDTO.getTotalAmount());
					updateAmount.setMasterAmount(zsChannelPrepayDTO.getTotalAmount());
				}
				updateAmount.setMasterPayStatus(PayConsts.PAY_STATUS_NONE);
				updateAmount.setPayStatus(PayConsts.PAY_STATUS_NONE);
			}
		}

		// 保存优惠
		if(updateAmount.getDiscountAmount() != null && updateAmount.getDiscountAmount() > 0){
			List<OrderDiscount> orderDiscountList = Lists.newArrayList();
			OrderDiscount orderDiscount = new OrderDiscount();
			orderDiscount.setOrderId(updateAmount.getOrderId());
			orderDiscount.setCateg(FicoConsts.ORDER_IN_DISCOUNT_CATEG_CHANNEL);
			orderDiscount.setType(OrderConsts.ORDER_DISCOUNT_TYPE_LIMIT_FREE);
			orderDiscount.setAmount(updateAmount.getDiscountAmount());
			orderDiscount.setCreater("渠道对接");
			orderDiscount.setRemark("渠道优惠");
			orderDiscount.setCreateTime(new Date());
			orderDiscountList.add(orderDiscount);
			orderDiscountService.insertOrderDiscountList(orderDiscountList);
		}

		orderWorkAmountService.updatePayStatusChannelPrepay(updateAmount);
	}

}
